package com.youlai.boot.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.core.security.util.SecurityUtils;
import com.youlai.boot.system.converter.SysDianciFinishedRetrospectConverter;
import com.youlai.boot.system.mapper.SysDianciFinishedRetrospectMapper;
import com.youlai.boot.system.model.entity.SysDianciFinishedRetrospect;
import com.youlai.boot.system.model.form.SysDianciFinishedRetrospectForm;
import com.youlai.boot.system.model.query.SysDianciFinishedRetrospectQuery;
import com.youlai.boot.system.model.vo.SysDianciFinishedRetrospectVO;
import com.youlai.boot.system.model.vo.UserInfoVO;
import com.youlai.boot.system.service.SysDianciFinishedRetrospectService;
import com.youlai.boot.system.service.UserService;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 成品追溯服务实现类
 *
 * @author liwenbing
 * @since 2025-04-09 23:40
 */
@Service
@RequiredArgsConstructor
public class SysDianciFinishedRetrospectServiceImpl extends ServiceImpl<SysDianciFinishedRetrospectMapper, SysDianciFinishedRetrospect> implements SysDianciFinishedRetrospectService {

    private final SysDianciFinishedRetrospectConverter sysDianciFinishedRetrospectConverter;

    @Resource
    private UserService userService;

    /**
     * 获取成品追溯分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<SysDianciFinishedRetrospectVO>} 成品追溯分页列表
     */
    @Override
    public IPage<SysDianciFinishedRetrospectVO> getSysDianciFinishedRetrospectPage(SysDianciFinishedRetrospectQuery queryParams) {
        Page<SysDianciFinishedRetrospectVO> pageVO = this.baseMapper.getSysDianciFinishedRetrospectPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }

    /**
     * 获取成品追溯表单数据
     *
     * @param id 成品追溯ID
     * @return
     */
    @Override
    public SysDianciFinishedRetrospectForm getSysDianciFinishedRetrospectFormData(Long id) {
        SysDianciFinishedRetrospect entity = this.getById(id);
        return sysDianciFinishedRetrospectConverter.toForm(entity);
    }

    /**
     * 新增成品追溯
     *
     * @param formData 成品追溯表单对象
     * @return
     */
    @Override
    public boolean saveSysDianciFinishedRetrospect(SysDianciFinishedRetrospectForm formData) {
        SysDianciFinishedRetrospect entity = sysDianciFinishedRetrospectConverter.toEntity(formData);
        UserInfoVO currentUserInfo = userService.getCurrentUserInfo();
        if (currentUserInfo != null) {
            entity.setUserId(currentUserInfo.getUserId());
            entity.setUserName(currentUserInfo.getNickname());

            entity.setCreateBy(currentUserInfo.getUserId());
            entity.setUpdateBy(currentUserInfo.getUserId());

        }
        entity.setCreateTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());
        return this.save(entity);
    }

    /**
     * 更新成品追溯
     *
     * @param id       成品追溯ID
     * @param formData 成品追溯表单对象
     * @return
     */
    @Override
    public boolean updateSysDianciFinishedRetrospect(Long id, SysDianciFinishedRetrospectForm formData) {
        SysDianciFinishedRetrospect entity = sysDianciFinishedRetrospectConverter.toEntity(formData);
        Long userId = SecurityUtils.getUserId();
        entity.setUpdateBy(userId);
        entity.setUpdateTime(LocalDateTime.now());
        return this.updateById(entity);
    }

    /**
     * 删除成品追溯
     *
     * @param ids 成品追溯ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteSysDianciFinishedRetrospects(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的成品追溯数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
